route.test.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // app/api/branches/[branch]/[year]/months/route.test.js
  2. import { describe, it, expect, beforeAll, afterAll } from "vitest";
  3. import fs from "node:fs/promises";
  4. import os from "node:os";
  5. import path from "node:path";
  6. import { GET as getMonths } from "./route.js";
  7. let tmpRoot;
  8. beforeAll(async () => {
  9. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-months-"));
  10. process.env.NAS_ROOT_PATH = tmpRoot;
  11. // tmpRoot/NL01/2024/10
  12. await fs.mkdir(path.join(tmpRoot, "NL01", "2024", "10"), {
  13. recursive: true,
  14. });
  15. });
  16. afterAll(async () => {
  17. await fs.rm(tmpRoot, { recursive: true, force: true });
  18. });
  19. describe("GET /api/branches/[branch]/[year]/months", () => {
  20. it("returns months for a valid branch/year", async () => {
  21. const req = new Request("http://localhost/api/branches/NL01/2024/months");
  22. const ctx = {
  23. params: Promise.resolve({ branch: "NL01", year: "2024" }),
  24. };
  25. const res = await getMonths(req, ctx);
  26. expect(res.status).toBe(200);
  27. const body = await res.json();
  28. expect(body).toEqual({
  29. branch: "NL01",
  30. year: "2024",
  31. months: ["10"],
  32. });
  33. });
  34. it("returns 400 when branch or year is missing", async () => {
  35. const req = new Request("http://localhost/api/branches/NL01/2024/months");
  36. const ctx = {
  37. params: Promise.resolve({ branch: "NL01" }), // year missing
  38. };
  39. const res = await getMonths(req, ctx);
  40. expect(res.status).toBe(400);
  41. const body = await res.json();
  42. expect(body.error).toBe("branch oder year fehlt");
  43. });
  44. });